package com.xuecheng.media.api;

import com.xuecheng.base.utils.RestResponse;
import com.xuecheng.media.model.dto.UploadFileParamsDto;
import com.xuecheng.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @ClassName BigFileController
 * @Description TODO
 * @Author kiku
 * @Date 2024/9/16 21:17
 * @Version 1.0
 */
@RestController
public class BigFileController {

    @Autowired
    private MediaFileService mediaFileService;

    /**
     * @author Mr.M
     * @version 1.0
     * @description 大文件上传接口
     * @date 2022/9/6 11:29
     */
    @Api(value = "大文件上传接口", tags = "大文件上传接口")
    @RestController
    public class BigFilesController {



        @ApiOperation(value = "文件上传前检查文件")
        @PostMapping("/upload/checkfile")
        public RestResponse<Boolean> checkfile(
                @RequestParam("fileMd5") String fileMd5
        ) throws Exception {
            RestResponse<Boolean> booleanRestResponse = mediaFileService.checkFile(fileMd5);

            return booleanRestResponse;
        }


        @ApiOperation(value = "分块文件上传前的检测")
        @PostMapping("/upload/checkchunk")
        public RestResponse<Boolean> checkchunk(@RequestParam("fileMd5") String fileMd5,
                                                @RequestParam("chunk") int chunk) throws Exception {

            RestResponse<Boolean> booleanRestResponse = mediaFileService.checkChunk(fileMd5, chunk);

            return booleanRestResponse;
        }

        @ApiOperation(value = "上传分块文件")
        @PostMapping("/upload/uploadchunk")
        public RestResponse uploadchunk(@RequestParam("file") MultipartFile file,
                                        @RequestParam("fileMd5") String fileMd5,
                                        @RequestParam("chunk") int chunk) throws Exception {
            File tempFile = File.createTempFile("tempchunk", ".temp");
            file.transferTo(tempFile);
            String absolutePath = tempFile.getAbsolutePath();

            return mediaFileService.uploadChunk(fileMd5,chunk,absolutePath);
        }

        @ApiOperation(value = "合并文件")
        @PostMapping("/upload/mergechunks")
        public RestResponse mergechunks(@RequestParam("fileMd5") String fileMd5,
                                        @RequestParam("fileName") String fileName,
                                        @RequestParam("chunkTotal") int chunkTotal) throws Exception {
            Long companyId = 1232141425L;
            UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();

            uploadFileParamsDto.setTags("视频文件");
            uploadFileParamsDto.setFileType("001002");
            uploadFileParamsDto.setFilename(fileName);
            RestResponse mergechunks = mediaFileService.mergechunks(companyId, fileMd5, chunkTotal, uploadFileParamsDto);


            return mergechunks;

        }


    }
}
